import { Alert } from "../../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { AcupointData } from "./AcupointData";
import { Cache } from "../../../cache/Cache";
import { Language } from "../../../../../extend/language/Language";
import { SAttributeUpdate } from "../../../../../Message/Public/SAttributeUpdate";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { AcupointConfig } from "../../../resource/AcupointConfig";
import { GameConst } from "../../../../component/gconst/GameConst";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { ObjCreate } from "../../common/ObjCreate";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { PropItem } from "./PropItem";
import { EquipPropButton } from "../../equipment/deploy/EquipPropButton";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ShopConfig } from "../../../resource/ShopConfig";
import { ShopRule } from "../../../rules/ShopRule";
import { ItemData } from "../../../resource/info/ItemData";
import { ItemConfig } from "../../../resource/ItemConfig";
import { QuickBuyWindow } from "../../common/QuickBuyWindow";
import { MsgManager } from "../../../manager/MsgManager";
import { TextInput } from "../../../../../fl/controls/TextInput";
import { GNumericStepper } from "../../../../../com/mui/controls/GNumericStepper";
import { ShopItemInfo } from "../../../resource/info/ShopItemInfo";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { _step } from "../../../../../this/_step";
import { _shopItemInfo } from "../../../../../this/_shopItemInfo";
type int = number;
//class AcupointRootUpperUpgrade
    
    export  class AcupointRootUpperUpgrade /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.init();
            return;
        }

        /* internal  */onUpgradeAlertCloseHandler(arg1: int): void
        {
            if (Alert.OK == arg1) 
            {
                this.upgradeRoot();
            }
            Alert.resetToDefault();
            return;
        }

        /* internal  */onAlertCloseHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointRootNeedBreak, this._step));
            }
            Alert.resetToDefault();
            return;
        }

        public updateAttInfo(arg1: int): void
        {
            var loc12=0;
            var loc13=0;
            this._step = arg1;
            var loc1=AcupointData.getAttributesByStep(arg1, 1, AcupointData.acupointInfo.attributes);
            var loc2=AcupointData.getAttributesByStep(arg1, 3, AcupointData.acupointInfo.attributes);
            var loc3=Cache.instance.role.careerBasic;
            var loc4=Cache.instance.role.careerEnabled;
            var loc5=AcupointData.getAttributeStr(loc1, "", loc3, loc4, true, false);
            var loc6=AcupointData.getAttributeStr(loc2, "", loc3, loc4, true, false);
            var loc7;
            var loc8=(loc7 = AcupointData.getRootLevel(arg1)) + 1;
            this._currentTxt.text = Language.getStringByParam(30218, loc7) + loc5;
            if (loc8 <= AcupointData.maxRoot) 
            {
                this._nextTxt.text = Language.getStringByParam(30219, loc8) + loc6;
            }
            else 
            {
                this._nextTxt.text = Language.getString(30220);
            }
            var loc9;
            var loc10=(loc9 = AcupointData.acupointInfo.rootEffects)[arg1];
            var loc11;
            if (loc11 = loc1[0] as SAttributeUpdate) 
            {
                loc13 = (loc12 = loc11.value) / (loc10 + 100) * 100;
                this._addDesTxt.text = Language.getString(30257) + loc13 + "\t" + Language.getString(30258) + (loc12 - loc13);
            }
            return;
        }

        public updateConfigInfo(arg1: int): void
        {
            this.updateAttInfo(arg1);
            this._useLukyPropBtn.visible = true;
            this._luckPropItem.visible = false;
            this._luckPropItem.filters = [FilterConst.colorFilter];
            this._lukyPropNumStepper.value = 0;
            var loc1=AcupointData.getRootLevel(arg1);
            var loc2=AcupointConfig.instance.getRootInfoByTarget(loc1);
            this._propNum = loc2.num;
            this._rate = loc2.rate;
            this._cost = loc2.cost;
            this._unit = loc2.unit;
            this._costTxt.text = this._cost + "";
            this._rateTxt.text = this._rate + "%";
            this._propNumInput.text = this._propNum + "";
            if (loc1 >= 100 && loc1 < 110) 
            {
                this._perProtectAddRate = GameConst.UpgradeRootProtectAddRate;
            }
            else if (loc1 >= 100 && loc1 < 120) 
            {
                this._perProtectAddRate = GameConst.UpgradeRootProtectAddRateEx;
            }
            else if (loc1 >= 120 && loc1 < 130) 
            {
                this._perProtectAddRate = GameConst.UpgradeRootProtectAddRate130;
            }
            this.updateProtectProp();
            return;
        }

        public updateProtectProp(): void
        {
            var loc4=false;
            this._protectPropNum = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootProtect);
            var loc1=Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootProp);
            this._propItem.setAmountText(loc1);
            if (this._isAutoBuy && loc1 > 0) 
            {
                this._isAutoBuy = false;
                loc4 = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointUpgradeRoot, {"step": _step, "useProtectNum": this._lukyPropNumStepper.value, "perfectUpgrade": loc4}));
            }
            if (this._protectPropNum <= 0 && this._luckPropItem.visible) 
            {
                this._luckPropItem.filters = [FilterConst.colorFilter];
                this._luckPropItem.visible = false;
                this._useLukyPropBtn.visible = true;
            }
            else if (this._luckPropItem.visible) 
            {
                this._luckPropItem.filters = [];
                this._useLukyPropBtn.visible = false;
            }
            var loc2=Math.ceil((100 - this._rate) / this._perProtectAddRate);
            if (this._protectPropNum < loc2) 
            {
                loc2 = this._protectPropNum;
            }
            this._lukyPropNumStepper.maximum = loc2;
            var loc3=this._rate + this._perProtectAddRate * this._lukyPropNumStepper.value;
            if (loc3 > 100) 
            {
                loc3 = 100;
            }
            this._rateTxt.text = loc3 + "%";
            return;
        }

        /* internal  */init(): void
        {
            var loc7=null;
            var loc8=0;
            ObjCreate.createBg(0, 0, 279, 395, this);
            addChild(new GImageBitmap(ImagesConst.RootBreakBg, 2, 55));
            addChild(new GImageBitmap(ImagesConst.RootBreakArrow, 110, 5));
            ObjCreate.createBg(5, 55, 264, 2, this, ImagesConst.SplitLine);
            ObjCreate.createBg(5, 260, 264, 2, this, ImagesConst.SplitLine);
            addChild(new GImageBitmap("equipment_successRate.png", 100, 67));
            ObjCreate.createBg(80, 100, 110, 100, this, ImagesConst.WindowCenterC);
            var loc1=new GTextFormat(FontUtil.xingkaiName, 34, 65280, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._rateTxt = ObjCreate.createTextField("", 80, 130, 110, 60, this, loc1);
            var loc2=new Object /* flash.text.TextFormat */("", 12, 15788607, null, null, null, null, null, null, null, null, 3);
            this._currentTxt = ObjCreate.createTextField("", 15, 5, 150, 40, this, loc2);
            this._currentTxt.multiline = true;
            this._nextTxt = ObjCreate.createTextField("", 170, 5, 150, 40, this, loc2);
            this._nextTxt.multiline = true;
            var loc3=new GTextFormat("", 12, 16750848);
            this._addDesTxt = ObjCreate.createTextField("", 15, 37, 260, 20, this, loc3);
            this._propItem = new PropItem();
            this._propItem.x = 20;
            this._propItem.y = 100;
            this._propItem.setSize(33, 33);
            addChild(this._propItem);
            this._propNumInput = ObjCreate.createGTextInput(20, 140, 40, 22, this);
            this._propNumInput.editable = false;
            var loc4;
            (loc4 = ObjCreate.createGButton(Language.getString(30124), 20, 168, 50, 23, this)).addEventListener(flash.events.MouseEvent.CLICK, this.onBuyBtnClickHandler);
            this._useLukyPropBtn = new EquipPropButton(Language.getString(30214));
            this._useLukyPropBtn.x = 200;
            this._useLukyPropBtn.y = 100;
            this._useLukyPropBtn.useHandCursor = true;
            this._useLukyPropBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onUseLuckPropBtnClickHandler);
            addChild(this._useLukyPropBtn);
            this._luckPropItem = new PropItem();
            this._luckPropItem.x = 200;
            this._luckPropItem.y = 100;
            this._luckPropItem.setSize(33, 33);
            this._luckPropItem.visible = false;
            this._luckPropItem.addEventListener(flash.events.MouseEvent.CLICK, this.onLuckPropItemClickHandler);
            addChild(this._luckPropItem);
            this._lukyPropNumStepper = ObjCreate.createGNumericStepper(200, 140, 45, 22, this, 99, 0);
            this._lukyPropNumStepper.value = 0;
            this._lukyPropNumStepper.addEventListener(flash.events.Event.CHANGE, this.onNumStepperChangeHandler);
            this._autoBuyCb = ObjCreate.createCheckBox(Language.getString(30128), 85, 205, 200, 20, this);
            var loc5;
            (loc5 = ObjCreate.createGButton(Language.getString(30104), 150, 230, 80, 23, this)).addEventListener(flash.events.MouseEvent.CLICK, this.onUpgradeBtnClickHandler);
            ObjCreate.createTextField(Language.getString(30215), 25, 232, 50, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createBg(60, 232, 80, 20, this, ImagesConst.InputBg);
            this._costTxt = ObjCreate.createTextField("0", 60, 232, 80, 20, this, GlobalStyle.textFormat2);
            this._costImg = new Object /* flash.display.Bitmap */();
            this._costImg.x = 120;
            this._costImg.y = 234;
            addChild(this._costImg);
            this._costImg.bitmapData = GlobalClass.getBitmapData(ImagesConst.Tongqian_small);
            this._shopItemInfo = ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropRootProp, 1, -1, true, ShopRule.ShopMallProp);
            if (this._shopItemInfo != null) 
            {
                loc7 = new ItemData(this._shopItemInfo.itemInfo.item.code);
                this._propItem.itemData = loc7;
                loc8 = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootProp);
                this._propItem.setAmountText(loc8);
            }
            var loc6;
            if ((loc6 = ItemConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropRootProtect)) && loc6.item) 
            {
                this._luckPropItem.itemData = new ItemData(loc6.item.code);
            }
            this._luckPropItem.filters = [FilterConst.colorFilter];
            return;
        }

        /* internal  */onBuyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropRootProp, 1, -1, true, ShopRule.ShopMallProp);
            QuickBuyWindow.instance.updateDefaultAmount(this._propNum);
            QuickBuyWindow.instance.show();
            return;
        }

        /* internal  */onLuckPropItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._useLukyPropBtn.visible = true;
            this._luckPropItem.visible = false;
            this._luckPropItem.filters = [];
            this._lukyPropNumStepper.value = 0;
            var loc1=this._rate + this._perProtectAddRate * this._lukyPropNumStepper.value;
            if (loc1 > 100) 
            {
                loc1 = 100;
            }
            this._rateTxt.text = loc1 + "%";
            return;
        }

        /* internal  */onUseLuckPropBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=0;
            var loc1=AcupointData.getRootLevel(this._step);
            if (loc1 >= AcupointData.maxRoot) 
            {
                MsgManager.showRollTipsMsg(Language.getString(30227));
                return;
            }
            this._protectPropNum = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootProtect);
            if (this._protectPropNum > 0) 
            {
                this._useLukyPropBtn.visible = false;
                this._luckPropItem.visible = true;
                this._luckPropItem.filters = [];
                if (this._rate < 100) 
                {
                    this._lukyPropNumStepper.value = 1;
                    loc2 = this._rate + this._perProtectAddRate * this._lukyPropNumStepper.value;
                    if (loc2 > 100) 
                    {
                        loc2 = 100;
                    }
                    this._rateTxt.text = loc2 + "%";
                }
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(30216));
            }
            return;
        }

        /* internal  */onNumStepperChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._useLukyPropBtn.visible) 
            {
                this._lukyPropNumStepper.value = 0;
                return;
            }
            var loc1=this._rate + this._perProtectAddRate * this._lukyPropNumStepper.value;
            if (loc1 > 100) 
            {
                loc1 = 100;
            }
            this._rateTxt.text = loc1 + "%";
            if (this._lukyPropNumStepper.value == 0) 
            {
                this._useLukyPropBtn.visible = true;
                this._luckPropItem.visible = false;
                this._luckPropItem.filters = [];
            }
            return;
        }

        /* internal  */onUpgradeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc4=0;
            var loc1=AcupointData.getRootLevel(this._step);
            var loc2=AcupointData.getRootBreakThroughsLevelByStep(this._step);
            if (AcupointConfig.instance.isCanBreakThrough(loc1) && loc1 == loc2) 
            {
                Alert.show(Language.getString(30217), null, Alert.OK | Alert.CANCEL, null, this.onAlertCloseHandler);
                return;
            }
            if (loc1 >= AcupointData.maxRoot) 
            {
                MsgManager.showRollTipsMsg(Language.getString(30227));
                return;
            }
            if (this._lukyPropNumStepper.value < Math.ceil((100 - this._rate) / this._perProtectAddRate) && this._luckPropItem.visible) 
            {
                Alert.show(Language.getString(30255), null, Alert.OK | Alert.CANCEL, null, this.onUpgradeAlertCloseHandler);
                return;
            }
            var loc3;
            if ((loc3 = Cache.instance.role.money.coin + Cache.instance.role.money.coinBind) < this._cost) 
            {
                loc4 = this._cost - loc3;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc4));
                return;
            }
            this.upgradeRoot();
            return;
        }

        /* internal  */upgradeRoot(): void
        {
            var loc2=0;
            var loc3=false;
            var loc1=Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropRootProp);
            if (loc1 < this._propNum) 
            {
                if (this._autoBuyCb.selected) 
                {
                    this._isAutoBuy = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": _shopItemInfo, "amount": Math.abs(this._propNum - loc1)}));
                }
                else 
                {
                    QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropRootProp, 1, -1, true, ShopRule.ShopMallProp);
                    QuickBuyWindow.instance.updateDefaultAmount(this._propNum - loc1);
                    QuickBuyWindow.instance.show();
                }
                return;
            }
            if (Cache.instance.role.money.coin + Cache.instance.role.money.coinBind < this._cost) 
            {
                loc2 = this._cost - (Cache.instance.role.money.coin + Cache.instance.role.money.coinBind);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, loc2));
            }
            else 
            {
                loc3 = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.AcupointUpgradeRoot, {"step": _step, "useProtectNum": this._lukyPropNumStepper.value, "perfectUpgrade": loc3}));
            }
            return;
        }

        /* internal */ /* var */_currentTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_nextTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_addDesTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_useLukyPropBtn: EquipPropButton;

        /* internal */ /* var */_luckPropItem: PropItem;

        /* internal */ /* var */_propItem: PropItem;

        /* internal */ /* var */_propNumInput: TextInput;

        /* internal */ /* var */_lukyPropNumStepper: GNumericStepper;

        /* internal */ /* var */_costTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_costImg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_rateTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_propNum: int;

        /* internal */ /* var */_rate: int;

        /* internal */ /* var */_cost: int;

        /* internal */ /* var */_unit: int;

        /* internal */ /* var */_step: int;

        /* internal */ /* var */_protectPropNum: int;

        /* internal */ /* var */_isAutoBuy: Boolean=false;

        /* internal */ /* var */_perProtectAddRate: int;

        /* internal */ /* var */_shopItemInfo: ShopItemInfo;

        /* internal */ /* var */_autoBuyCb: GCheckBox;
    }
